package com.caijinbang.study.base;

import com.caijinbang.study.utils.MongoDbUtils;
import com.caijinbang.study.utils.access.MongoAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * mongo基础抽象接口类
 *
 * @author caijb
 * @version 1.0.0
 * @date 2018-05-03 12:35
 */
public abstract class BaseMongoDao {

    @Autowired
    private MongoAccess mongoAccess;

    /**
     * 根据id获取对象
     *
     * @param id
     * @param entityClass
     * @param includeKeys
     * @param <T>
     * @return
     */
    public <T> T get(Object id, Class<T> entityClass, String... includeKeys) {
        Query query = new Query(where("_id").is(id));
        MongoDbUtils.addIncludeKeys(query, includeKeys);
        return mongoAccess.findOne(query, entityClass);
    }

    /**
     * 根据查询条件查找
     *
     * @param query
     * @param entityClass
     * @param includeKeys
     * @param <T>
     * @return
     */
    public <T> List<T> find(Query query, Class<T> entityClass, String... includeKeys) {
        MongoDbUtils.addIncludeKeys(query, includeKeys);
        return mongoAccess.find(query, entityClass);
    }

    /**
     * 根据id集合查询
     *
     * @param ids
     * @param entityClass
     * @param includeKeys
     * @param <T>
     * @return
     */
    public <T> List<T> findByIds(List<?> ids, Class<T> entityClass, String... includeKeys) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        Query query = new Query(where("_id").in(ids));
        MongoDbUtils.addIncludeKeys(query, includeKeys);
        return mongoAccess.find(query, entityClass);
    }

    /**
     * 根据条件统计
     *
     * @param query
     * @param entityClass
     * @return
     */
    public long count(Query query, Class<?> entityClass) {
        return mongoAccess.count(query, entityClass);
    }

    /**
     * 保存
     *
     * @param entity
     */
    public void save(Object entity) {
        mongoAccess.save(entity);
    }

    /**
     * 批量保存
     *
     * @param entities
     * @param entityClass
     */
    public void batchInsert(Collection<? extends Object> entities, Class<?> entityClass) {
        mongoAccess.batchInsert(entities, entityClass);
    }

    /**
     * 删除实体
     *
     * @param entity
     */
    public void delete(Object entity) {
        mongoAccess.delete(entity);
    }

    /**
     * 根据id删除实体
     *
     * @param update
     * @param id
     * @param entityClass
     * @param <T>
     */
    public <T> void updateById(Update update, Object id, Class<T> entityClass) {
        mongoAccess.updateFirst(new Query().addCriteria(where("_id").is(id)), update, entityClass);
    }

    /**
     * 根据id集合更新
     *
     * @param update
     * @param ids
     * @param entityClass
     * @param <T>
     */
    public <T> void updateByIds(Update update, List<Integer> ids, Class<T> entityClass) {
        mongoAccess.updateMulti(new Query().addCriteria(where("_id").in(ids)), update, entityClass);
    }
}
